<?php

/**
 * Specify book object. Book table.
 */
class book
{
	/**
	 * int book id
	 */
	var $id;
	
	/**
	 * string book title
	 */
	var $title;
	
	/**
	 * string book author
	 */
	var $author;
	
	/**
	 * string book copyright
	 */
	var $copyright;
	
	/**
	 * string book description
	 */
	var $description;
	
	/**
	 * string book header
	 */
	var $header;
	
	/**
	 * string book footer
	 */
	var $footer;
	
	/**
	 * string book theme
	 */
	var $theme;
	
	/**
	 * string book contents
	 */
	var $contents;
	
	/** 
	 * string book mediastorage folder
	 */
	var $mediastorage;
}

/**
 * Get books.
 * 
 * @param $p_sort_field string sort field
 * @param $p_sort_order string sort order: ASC or DESC
 * @param $p_page int page number
 * @param $p_count int items count on the page
 * @param $p_filter class book_filter
 *
 * @return array books array
 */
function get_books($p_sort_field = 'id', 
			$p_sort_order = 'ASC', 
			$p_page = 1, 
			$p_count = 0,
			$p_filter = NULL
			)
{
	$t_sort_field = db_escape_string($p_sort_field);
	$t_sort_order = db_prepare_sort_order($p_sort_order);
	$t_page = db_prepare_int($p_page);
	$t_count = db_prepare_int($p_count);

	if ($t_sort_field == '')
		$t_sort_field = 'id';
		
	if (!in_array($t_sort_field, array('id', 'book_title')))
	{
		$t_sort_field = 'id';
	}
	
	$order_str = "`$t_sort_field` $t_sort_order";
	
	if ($p_count != 0)
	{
		$limit = ($t_page-1)*$t_count; 
		$limit_str = "$limit, $t_count";
	}
	else
	{
		$limit_str = '';
	}
	
	$tmp =  ($p_filter != NULL) ? $p_filter->query() : '';
	
	$books = db_extract(DB_TABLE_BOOK, $tmp, 
			$t_sort_field.' '. $t_sort_order, $limit_str);
	
	return $books;
}

/**
 * Get books from books id array.
 * 
 * @param $p_id_array int array books id
 *
 * @return array books array
 */
function get_books_from_array($p_id_array)
{
	$tmp = '';
	$array = array_values($p_id_array);
	$size = sizeof($array);
	
	if ($size == 0)
		return NULL;

	for($i = 0; $i < $size - 1; $i++)
		$tmp .=  db_prepare_int($array[$i]).', ';
	$tmp .= db_prepare_int($array[$size-1]);
	
	global $WEB_APP;
	
	$query = 'SELECT *
		 FROM '.DB_TABLE_BOOK.
		' WHERE '.DB_TABLE_BOOK.'.`id` in ('.$tmp.')
		ORDER BY '.DB_TABLE_BOOK.'.`id` ASC';			
			
	$books = db_query($query); 
	return $books;
}

/** 
 * Move book to course_id.
 *
 * @param $p_course_id int new course id
 * @param $p_book_course_id int book_course id
 *
 * @return bool TRUE on success or FALSE on failure
 */
function move_book($p_course_id, $p_book_course_id)
{
	$t_course_id = db_prepare_int($p_course_id);
	$t_book_course_id = db_prepare_int($p_book_course_id);
	
	$query = 'UPDATE '.DB_TABLE_BOOK_COURSE.
		 ' SET `course_id` = '.$t_course_id.
		 ' WHERE `id` = '.$t_book_course_id;
		 
	db_exec($query);
	
	return (db_last_error() == '');	
}

/**
 * Move books to over course.
 *
 * @param $p_course_id int course id
 * @param $p_id_array int books id array
 *
 * @return bool TRUE on success or FALSE on failure
 */
function move_books($p_course_id, $p_id_array)
{	
	$tmp = TRUE;
	
	foreach($p_id_array as $p_id)
	{
		$result = move_book($p_course_id, $p_id);
		$tmp = $tmp && $result;
	}
	
	return $tmp;
}

/**
 * Get books count by filter.
 *
 * @param $p_filter class book_filter
 *
 * @return int books count
 */
function get_books_count($p_filter)
{
	$tmp =  ($p_filter != NULL) ? $p_filter->query() : '';
	if ($tmp != '')
	{
		$tmp = ' WHERE '.$tmp;
	}
	
	$query = 'SELECT COUNT(*) as `_count_`
			 FROM '.DB_TABLE_BOOK.$tmp;
	$result = db_query($query);
	
	return isset($result[0]['_count_']) ? $result[0]['_count_'] : 0;
}

/**
 * Get book id by title.
 *
 * @param $p_title string book title
 *
 * @return int book id on success or 0 on failure
 */
function get_book_id($p_title)
{
	$t_title = db_prepare_string($p_title);
	
	$query = 'SELECT `id`
		FROM '.DB_TABLE_BOOK.
		' WHERE `book_title`='.$t_title;
		
	$result = db_query($query);

	return isset($result[0]['id']) ? $result[0]['id'] : 0;
}

/**
 * Get book by book id.
 *
 * @param $p_id int book id
 *
 * @return book class
 */
function get_book($p_id)
{
	$t_id = db_prepare_int($p_id);
	
	$books[] = db_extract(DB_TABLE_BOOK, '`id` = '.$t_id);
	
	$book = new book();
	
	if (count($books[0]) > 0)
	{
		$book->id 		    = $books[0][0]['id'];
		$book->title 		= $books[0][0]['book_title'];
		$book->author 		= $books[0][0]['book_author'];
		$book->copyright 	= $books[0][0]['book_copyright'];
		$book->description 	= $books[0][0]['book_description'];
		$book->header 		= $books[0][0]['book_header'];
		$book->footer 		= $books[0][0]['book_footer'];
		$book->theme 		= $books[0][0]['book_theme'];
		$book->contents 	= $books[0][0]['book_contents'];
		$book->mediastorage = $books[0][0]['book_mediastorage'];
	}
	
	return $book;
}

/**
 * Delete book id with/without book courses.
 *
 * @param $p_id int book id
 * @param $p_without_book_course bool TRUE - with book courses
 *
 * @return bool TRUE on success or FALSE on failure
 */
function delete_book($p_id, $p_without_book_course = FALSE)
{
	$t_id = db_prepare_int($p_id);	
	
	if ($p_without_book_course)
	{
		$count = 0;
	}
	else
	{
		$count = db_count(DB_TABLE_BOOK_COURSE, '`book_id` = '.$t_id);
	}
	
	if ($count == 0)
	{
		$b = delete_chapters($t_id);
		if ($b)
		{
			$query =  'DELETE FROM '.DB_TABLE_BOOK.
					' WHERE `id`='.$t_id;
			db_exec($query);
			return (db_last_error() == '');
		}
		
		return $b;
		
	}
	else
	{
		return FALSE;
	}
}

/**
 * Delete books.
 *
 * @param $p_id_array int array book id array
 *
 * @return bool TRUE on success or FALSE on failure
 */
function delete_books($p_id_array)
{
	$tmp = TRUE;
	foreach($p_id_array as $id)
	{
		$result =  delete_book($id);
		$tmp = $tmp && $result;
	}
	
	return $tmp;
}

/** 
 * Add book.
 *
 * @param $book class book object
 * @param $p_book_id int book id, NULL -don't use book id
 *
 * @return int book id on success or 0 on failure
 */
function add_book($book, $p_book_id = NULL)
{
	$book->title 		= db_prepare_string($book->title);
	$book->author 		= db_prepare_string($book->author);
	$book->copyright 	= db_prepare_string($book->copyright);
	$book->description 	= db_prepare_string($book->description);
	$book->header 		= db_prepare_string($book->header);
	$book->footer 		= db_prepare_string($book->footer);
	$book->theme 		= db_prepare_string($book->theme);
	$book->contents 	= db_prepare_string($book->contents);
	$book->mediastorage = db_prepare_string($book->mediastorage);
	if ($p_book_id != NULL)
	{
		delete_book($p_book_id, TRUE);
	}
	if ($p_book_id == NULL)
	{
		$t_book_id = 'NULL';
	}
	else
	{
		$t_book_id = db_prepare_int($p_book_id);
	}
	 
	$query = 'INSERT INTO '.DB_TABLE_BOOK.
			'(
			`id`,
			`book_title`, 
			`book_author`, 
			`book_copyright`,
			`book_description`, 
			`book_header`, 
			`book_footer`,
			`book_theme`, 
			`book_contents`, 
			`book_mediastorage`
			)
		VALUES
			('.
			$t_book_id.', '.
			$book->title.', '.
			$book->author.', '.
			$book->copyright.', '.
			$book->description.', '.
			$book->header.', '.
			$book->footer.', '.
			$book->theme.', '.
			$book->contents.', '.
			$book->mediastorage.
			')';
	
	$result = db_query($query);
	
	if (db_last_error() != '')
	{
		return 0;
	}
	
	if ($p_book_id == NULL)
	{
		return db_insert_id();
	}
	else
	{
		return $p_book_id;
	}
}

/**
 * Import book from xml file.
 *
 * @param $p_file string xml file
 * @param $p_book_id int book id, NULL -don't use book id
 *
 * @return int book id
 */
function import_book($p_file, $p_book_id = NULL)
{
	$file = CFG_BOOKS_DIR.$p_file;
	$items = get_xml_data($file);
	
	$book = new book();
	
	$book->title = get_value_array($items, 'SUNRAVBOOK/INFO/TITLE');
	$book->author = get_value_array($items, 'SUNRAVBOOK/INFO/AUTHOR');
	$book->copyright = get_value_array($items, 'SUNRAVBOOK/INFO/COPYRIGHT');
	$book->description = 
		get_value_array($items, 'SUNRAVBOOK/INFO/DESCRIPTION');
	$book->header = get_value_array($items, 'SUNRAVBOOK/INFO/HEADER');
	$book->footer = get_value_array($items, 'SUNRAVBOOK/INFO/FOOTER');
	$book->theme = get_value_array($items, 'SUNRAVBOOK/INFO/THEME');
	$book->contents = get_value_array($items, 'SUNRAVBOOK/INFO/CONTENTS');
	$book->mediastorage = get_value_array($items, 
		'SUNRAVBOOK/INFO/MEDIASTORAGE');
	
	$book_id = add_book($book, $p_book_id);
	import_chapters($book_id, $items);
	
	return $book_id;
}

/**
 * Edit book.
 *
 * @param $book book class
 *
 * @return adodb result
 */ 
function edit_book($book)
{
	$book->id 		    = db_prepare_int($book->id);
	$book->title 		= db_prepare_string($book->title);
	$book->author 		= db_prepare_string($book->author);
	$book->copyright 	= db_prepare_string($book->copyright);
	$book->description 	= db_prepare_string($book->description);
	$book->header 		= db_prepare_string($book->header);
	$book->footer 		= db_prepare_string($book->footer);
	$book->theme 		= db_prepare_string($book->theme);
	$book->mediastorage = db_prepare_string($book->mediastorage);

	$query = 'UPDATE '.DB_TABLE_BOOK.
		 ' SET 
		 `book_title`		= '.$book->title.',
		 `book_author` 		= '.$book->author.',
		 `book_copyright`	= '.$book->copyright.',
		 `book_description` 	= '.$book->description.',
		 `book_header` 		= '.$book->header.',
		 `book_footer` 		= '.$book->footer.',
		 `book_theme` 		= '.$book->theme.',
		 `book_mediastorage`	= '.$book->mediastorage.'
		  WHERE `id` 		='.$book->id;
		 
	return db_exec($query);
}

/**
 * Get books by user login and user password.
 *
 * @param $p_login string user login
 * @param $p_password string user password
 *
 * @return array books array
 */
function get_books_by_login_password($p_login, $p_password)
{
	$t_login = db_prepare_string($p_login);
	$t_password = db_prepare_string(md5($p_password));
	
	$query = 'SELECT DISTINCT `'.DB_TABLE_BOOK.'`.*
		FROM `'.
			DB_TABLE_BOOK.'`, `'.
			DB_TABLE_USER.'`, `'.
			DB_TABLE_GROUP.'`, `'.
			DB_TABLE_GROUP_COURSE.'`, `'.
			DB_TABLE_COURSE.'`, `'.
			DB_TABLE_BOOK_COURSE.
		'` WHERE	`'.DB_TABLE_USER.'`.`user_login` = '.$t_login.
		' AND `'.DB_TABLE_USER.'`.`user_password` = '.$t_password.
		' AND `'.DB_TABLE_USER.'`.`user_group_id` = `'.
			DB_TABLE_GROUP.'`.`id`'.
		' AND `'.DB_TABLE_GROUP_COURSE.'`.`group_id` = `'.
			DB_TABLE_GROUP.'`.`id`'.
		' AND `'.DB_TABLE_GROUP_COURSE.'`.`course_id` = `'.
			DB_TABLE_COURSE.'`.`id`'.
		' AND `'.DB_TABLE_BOOK_COURSE.'`.`course_id` = `'.
			DB_TABLE_COURSE.'`.`id`'.	
		' AND `'.DB_TABLE_BOOK_COURSE.'`.`book_id` = `'.
			DB_TABLE_BOOK.'`.`id`
		  AND `'.DB_TABLE_BOOK_COURSE.'`.`hidden` = 0';
	
	return db_query($query);
}

/**
 * Get books by course id.
 *
 * @param $p_course_id int course id
 *
 * @return array books array
 */
function get_books_by_course_id($p_course_id)
{
	$t_course_id = db_prepare_int($p_course_id);
	
	$query = 'SELECT '.DB_TABLE_BOOK.'.*
		  FROM '.DB_TABLE_BOOK.', '.DB_TABLE_BOOK_COURSE.
		  ' WHERE '.DB_TABLE_BOOK.'.`id` = '.
		  		DB_TABLE_BOOK_COURSE.'.`book_id`
		  AND '.DB_TABLE_BOOK_COURSE.'.`course_id` = '.$t_course_id.
		  ' AND '.DB_TABLE_BOOK_COURSE.'.`hidden` = 0';
	
	return db_query($query);
}

/**
 * Get books by course id, user login, and user password.
 *
 * @param $p_course_id int course id
 * @param $p_login string user login
 * @param $p_password string user password
 *
 * @return array books array
 */
function get_books_by_course_id_and_login_password($p_course_id, 
								   $p_login, 
								   $p_password
								   )
{
	$t_course_id = db_prepare_int($p_course_id);
	$t_login = db_prepare_string($p_login);
	$t_password = db_prepare_string(md5($p_password));
	
	$query = 'SELECT DISTINCT `'.DB_TABLE_BOOK.'`.*
		FROM `'.
			DB_TABLE_BOOK.'`, `'.
			DB_TABLE_USER.'`, `'.
			DB_TABLE_GROUP.'`, `'.
			DB_TABLE_GROUP_COURSE.'`, `'.
			DB_TABLE_COURSE.'`, `'.
			DB_TABLE_BOOK_COURSE.
		'` WHERE	`'.DB_TABLE_USER.'`.`user_login` = '.$t_login.
		' AND `'.DB_TABLE_USER.'`.`user_password` = '.$t_password.
		' AND `'.DB_TABLE_USER.'`.`user_group_id` = `'.
			DB_TABLE_GROUP.'`.`id`'.
		' AND `'.DB_TABLE_GROUP_COURSE.'`.`group_id` = `'.
			DB_TABLE_GROUP.'`.`id`'.
		' AND `'.DB_TABLE_GROUP_COURSE.'`.`course_id` = `'.
			DB_TABLE_COURSE.'`.`id`'.
		' AND `'.DB_TABLE_BOOK_COURSE.'`.`course_id` = `'.
			DB_TABLE_COURSE.'`.`id`'.	
		' AND `'.DB_TABLE_BOOK_COURSE.'`.`book_id` = `'.
			DB_TABLE_BOOK.'`.`id`
		  AND `'.DB_TABLE_BOOK_COURSE.'`.`hidden` = 0
		  AND `'.DB_TABLE_COURSE.'`.`id` = '.$t_course_id;
		  
	return db_query($query);
}
